package com.ticket.system.service.impl;

import com.google.common.collect.Maps;
import com.ticket.common.utils.DateUtils;
import com.ticket.common.utils.StringUtils;
import com.ticket.system.domain.YanchuOrder;
import com.ticket.system.domain.YanchuOrderLog;
import com.ticket.system.domain.YanchuPerform;
import com.ticket.system.mapper.YanchuOrderLogMapper;
import com.ticket.system.mapper.YanchuPerformMapper;
import com.ticket.system.service.IYanchuOrderLogService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-14
 */
@Service
public class YanchuOrderLogServiceImpl implements IYanchuOrderLogService 
{
    @Autowired
    private YanchuOrderLogMapper yanchuOrderLogMapper;
    @Autowired
    private YanchuPerformMapper yanchuPerformMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public YanchuOrderLog selectYanchuOrderLogById(Long id)
    {
        return yanchuOrderLogMapper.selectYanchuOrderLogById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param yanchuOrderLog 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<YanchuOrderLog> selectYanchuOrderLogList(YanchuOrderLog yanchuOrderLog)
    {
        return yanchuOrderLogMapper.selectYanchuOrderLogList(yanchuOrderLog);
    }

    @Override
    public List<Map<String,Object>> selectRecentOrderLogList(){
        List<YanchuOrderLog> list = yanchuOrderLogMapper.selectRecentOrderLogList();
        if(CollectionUtils.isNotEmpty(list)){
            for(YanchuOrderLog log:list){
                if(log.getTimeDiff() < 1){
                    log.setTimeTitle("1分钟内");
                }else if(log.getTimeDiff() >= 1 && log.getTimeDiff() < 60){
                    log.setTimeTitle(log.getTimeDiff()+"分钟前");
                }else if(log.getTimeDiff() > 60 && log.getTimeDiff() < 24*60){
                    log.setTimeTitle(log.getTimeDiff()/60+"小时前");
                }else if(log.getTimeDiff() >= 24*60){
                    log.setTimeTitle(DateUtils.monthDate(log.getCreateTime()));
                }
            }
        }
        List<Map<String,Object>> maps = Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(list)){
            Map<String,Object> map = null;
            StringBuilder sb = null;
            for(YanchuOrderLog log:list){
                map = Maps.newHashMap();
                sb = new StringBuilder();
                sb.append("用户").append(StringUtils.maskPhone(log.getPhone())).append("下单购买了《").append(log.getTitle()).append("》门票").append(log.getCount()).append("张");
                map.put("act",sb.toString());
                map.put("time",log.getTimeTitle());
                maps.add(map);
            }
        }
        return maps;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param yanchuOrderLog 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertYanchuOrderLog(YanchuOrderLog yanchuOrderLog)
    {
        yanchuOrderLog.setCreateTime(DateUtils.getNowDate());
        return yanchuOrderLogMapper.insertYanchuOrderLog(yanchuOrderLog);
    }

    @Override
    public int insertOrderLog(Long performId, Integer count, Long orderId, YanchuOrder yanchuOrder)
    {
        YanchuPerform perform = yanchuPerformMapper.selectYanchuPerformById(performId);
        if(null == perform){
            return 0;
        }
        YanchuOrderLog yanchuOrderLog = new YanchuOrderLog();
        yanchuOrderLog.setTitle(perform.getPerformName());
        yanchuOrderLog.setCount(Long.valueOf(count));
        if(StringUtils.isNotEmpty(yanchuOrder.getContactNumber())){
            yanchuOrderLog.setPhone(yanchuOrder.getContactNumber());
        }else{
            yanchuOrderLog.setPhone(yanchuOrder.getContacts());
        }

        yanchuOrderLog.setUserId(yanchuOrder.getUserId());
        yanchuOrderLog.setOrderId(orderId);
        return this.insertYanchuOrderLog(yanchuOrderLog);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param yanchuOrderLog 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateYanchuOrderLog(YanchuOrderLog yanchuOrderLog)
    {
        return yanchuOrderLogMapper.updateYanchuOrderLog(yanchuOrderLog);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteYanchuOrderLogByIds(Long[] ids)
    {
        return yanchuOrderLogMapper.deleteYanchuOrderLogByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteYanchuOrderLogById(Long id)
    {
        return yanchuOrderLogMapper.deleteYanchuOrderLogById(id);
    }
}
